﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibKetoan;
using LibDuoc;

namespace Ketoan
{
	/// <summary>
	/// Summary description for frmImpnhap.
	/// </summary>
	public class frmImpnhap : System.Windows.Forms.Form
	{
        private LibKetoan.AccessData a;
        private LibDuoc.AccessData d = new LibDuoc.AccessData();
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.DateTimePicker tu;
		private System.Windows.Forms.DateTimePicker den;
		private System.Windows.Forms.TextBox sophieu;
		private System.Windows.Forms.TextBox sohd;
		private System.Windows.Forms.TextBox madv;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.Button butLuu;
		private System.Windows.Forms.Button butKetthuc;
		private System.Windows.Forms.Button butTonghop;
		private int i_userd;
		private string s_mmyy,sql,s_ngay,format_sotien,user,sNoidung;
		//private AccessData a;
		private doiso.Doisototext doiso=new doiso.Doisototext();
		private DataSet ds=new DataSet();
		private DataSet dsct=new DataSet();
		private DataSet dsnhom=new DataSet();
		private DataSet dsxml=new DataSet();
		private DataTable dttk=new DataTable();
		private Brush disabledBackBrush;
		private Brush disabledTextBrush;
		private Brush alertBackBrush;
		private Font alertFont;
		private Brush alertTextBrush;
		private Font currentRowFont;
		private Brush currentRowBackBrush;
		private bool afterCurrentCellChanged,bStkhangnam,bClear=true;
        private int checkCol = 0, iHoatdong, iLctug, iChuong, iNghiepvu, iLoaibk, iLk, iCapphat, iChucnang = 5, iUserid, iTonghop = 1, iNhom, iTaikhoanchiuthue;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.ComboBox nhom;
		private System.Windows.Forms.DataGrid dataGrid2;
		private System.Windows.Forms.DataGrid dataGrid3;
		private System.Windows.Forms.CheckBox chkAll;
		private System.Windows.Forms.ToolTip toolTip1;
		private System.Windows.Forms.CheckBox chkChitiet;
		private System.Windows.Forms.ComboBox c710;
		private System.Windows.Forms.Label label42;
		private System.Windows.Forms.ComboBox c707;
		private System.Windows.Forms.Label label39;
		private System.Windows.Forms.ComboBox c706;
		private System.Windows.Forms.Label label50;
		private System.Windows.Forms.ComboBox c705;
		private System.Windows.Forms.Label label38;
		private System.Windows.Forms.ComboBox c703;
		private System.Windows.Forms.ComboBox c702;
		private System.Windows.Forms.Label label36;
		private System.Windows.Forms.Label label37;
		private System.Windows.Forms.ComboBox c700;
		private System.Windows.Forms.Label label35;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.ComboBox khoanmuc;
		private System.Windows.Forms.ComboBox khoanmuc2;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.ComboBox khoanmuc4;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.ComboBox khoanmuc3;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.Button butIn;
		private System.ComponentModel.IContainer components;
        private string sTaikhoanchiuthue;

		public frmImpnhap(LibKetoan.AccessData acc,string mmyy,string ngay,int userid)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			a=acc;s_mmyy=mmyy;i_userd=userid;s_ngay=ngay;
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
					if(disabledBackBrush != null)
					{
						disabledBackBrush.Dispose();
						disabledTextBrush.Dispose();
						alertBackBrush.Dispose();
						alertFont.Dispose();
						alertTextBrush.Dispose();
						currentRowFont.Dispose();
						currentRowBackBrush.Dispose();
					}
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmImpnhap));
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.tu = new System.Windows.Forms.DateTimePicker();
            this.den = new System.Windows.Forms.DateTimePicker();
            this.sophieu = new System.Windows.Forms.TextBox();
            this.sohd = new System.Windows.Forms.TextBox();
            this.madv = new System.Windows.Forms.TextBox();
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.butLuu = new System.Windows.Forms.Button();
            this.butKetthuc = new System.Windows.Forms.Button();
            this.butTonghop = new System.Windows.Forms.Button();
            this.label6 = new System.Windows.Forms.Label();
            this.nhom = new System.Windows.Forms.ComboBox();
            this.dataGrid2 = new System.Windows.Forms.DataGrid();
            this.dataGrid3 = new System.Windows.Forms.DataGrid();
            this.chkAll = new System.Windows.Forms.CheckBox();
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.chkChitiet = new System.Windows.Forms.CheckBox();
            this.c710 = new System.Windows.Forms.ComboBox();
            this.label42 = new System.Windows.Forms.Label();
            this.c707 = new System.Windows.Forms.ComboBox();
            this.label39 = new System.Windows.Forms.Label();
            this.c706 = new System.Windows.Forms.ComboBox();
            this.label50 = new System.Windows.Forms.Label();
            this.c705 = new System.Windows.Forms.ComboBox();
            this.label38 = new System.Windows.Forms.Label();
            this.c703 = new System.Windows.Forms.ComboBox();
            this.c702 = new System.Windows.Forms.ComboBox();
            this.label36 = new System.Windows.Forms.Label();
            this.label37 = new System.Windows.Forms.Label();
            this.c700 = new System.Windows.Forms.ComboBox();
            this.label35 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.khoanmuc = new System.Windows.Forms.ComboBox();
            this.khoanmuc2 = new System.Windows.Forms.ComboBox();
            this.label8 = new System.Windows.Forms.Label();
            this.khoanmuc4 = new System.Windows.Forms.ComboBox();
            this.label9 = new System.Windows.Forms.Label();
            this.khoanmuc3 = new System.Windows.Forms.ComboBox();
            this.label10 = new System.Windows.Forms.Label();
            this.butIn = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid3)).BeginInit();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(-8, 71);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(56, 23);
            this.label1.TabIndex = 24;
            this.label1.Text = "Tử ngày :";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(248, 73);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(32, 23);
            this.label2.TabIndex = 26;
            this.label2.Text = "đến :";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(369, 73);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(72, 23);
            this.label3.TabIndex = 28;
            this.label3.Text = "Số phiếu :";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(509, 71);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(72, 23);
            this.label4.TabIndex = 30;
            this.label4.Text = "Hóa đơn :";
            this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(642, 72);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(80, 23);
            this.label5.TabIndex = 32;
            this.label5.Text = "Đơn vị :";
            this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tu
            // 
            this.tu.CustomFormat = "dd/MM/yyyy";
            this.tu.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tu.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.tu.Location = new System.Drawing.Point(48, 72);
            this.tu.Name = "tu";
            this.tu.Size = new System.Drawing.Size(168, 21);
            this.tu.TabIndex = 12;
            this.tu.Value = new System.DateTime(2005, 5, 26, 11, 8, 30, 30);
            this.tu.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // den
            // 
            this.den.CustomFormat = "dd/MM/yyyy";
            this.den.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.den.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.den.Location = new System.Drawing.Point(280, 71);
            this.den.Name = "den";
            this.den.Size = new System.Drawing.Size(94, 21);
            this.den.TabIndex = 13;
            this.den.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // sophieu
            // 
            this.sophieu.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.sophieu.ForeColor = System.Drawing.Color.Red;
            this.sophieu.Location = new System.Drawing.Point(447, 72);
            this.sophieu.Name = "sophieu";
            this.sophieu.Size = new System.Drawing.Size(80, 21);
            this.sophieu.TabIndex = 229;
            this.sophieu.TextChanged += new System.EventHandler(this.sophieu_TextChanged);
            this.sophieu.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // sohd
            // 
            this.sohd.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.sohd.ForeColor = System.Drawing.Color.Red;
            this.sohd.Location = new System.Drawing.Point(587, 73);
            this.sohd.Name = "sohd";
            this.sohd.Size = new System.Drawing.Size(73, 21);
            this.sohd.TabIndex = 231;
            this.sohd.TextChanged += new System.EventHandler(this.sohd_TextChanged);
            this.sohd.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // madv
            // 
            this.madv.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.madv.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.madv.ForeColor = System.Drawing.Color.Red;
            this.madv.Location = new System.Drawing.Point(728, 73);
            this.madv.Name = "madv";
            this.madv.Size = new System.Drawing.Size(56, 21);
            this.madv.TabIndex = 213;
            this.madv.TextChanged += new System.EventHandler(this.madv_TextChanged);
            this.madv.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // dataGrid1
            // 
            this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.DataMember = "";
            this.dataGrid1.FlatMode = true;
            this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGrid1.Location = new System.Drawing.Point(8, 97);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.RowHeaderWidth = 5;
            this.dataGrid1.Size = new System.Drawing.Size(776, 193);
            this.dataGrid1.TabIndex = 124;
            this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
            this.dataGrid1.Click += new System.EventHandler(this.dataGrid1_Click);
            // 
            // butLuu
            // 
            this.butLuu.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butLuu.Image = ((System.Drawing.Image)(resources.GetObject("butLuu.Image")));
            this.butLuu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butLuu.Location = new System.Drawing.Point(318, 512);
            this.butLuu.Name = "butLuu";
            this.butLuu.Size = new System.Drawing.Size(74, 25);
            this.butLuu.TabIndex = 15;
            this.butLuu.Text = "&Cập nhật";
            this.butLuu.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butLuu.Click += new System.EventHandler(this.butLuu_Click);
            // 
            // butKetthuc
            // 
            this.butKetthuc.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butKetthuc.Image = ((System.Drawing.Image)(resources.GetObject("butKetthuc.Image")));
            this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butKetthuc.Location = new System.Drawing.Point(480, 512);
            this.butKetthuc.Name = "butKetthuc";
            this.butKetthuc.Size = new System.Drawing.Size(70, 25);
            this.butKetthuc.TabIndex = 16;
            this.butKetthuc.Text = "   &Kết thúc";
            this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
            // 
            // butTonghop
            // 
            this.butTonghop.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butTonghop.Image = ((System.Drawing.Image)(resources.GetObject("butTonghop.Image")));
            this.butTonghop.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butTonghop.Location = new System.Drawing.Point(242, 512);
            this.butTonghop.Name = "butTonghop";
            this.butTonghop.Size = new System.Drawing.Size(74, 25);
            this.butTonghop.TabIndex = 14;
            this.butTonghop.Text = "&Tổng hợp";
            this.butTonghop.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butTonghop.Click += new System.EventHandler(this.butTonghop_Click);
            // 
            // label6
            // 
            this.label6.Location = new System.Drawing.Point(224, 50);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(56, 23);
            this.label6.TabIndex = 22;
            this.label6.Text = "Bộ phận :";
            this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // nhom
            // 
            this.nhom.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.nhom.BackColor = System.Drawing.SystemColors.HighlightText;
            this.nhom.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.nhom.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.nhom.Location = new System.Drawing.Point(280, 51);
            this.nhom.Name = "nhom";
            this.nhom.Size = new System.Drawing.Size(504, 21);
            this.nhom.TabIndex = 11;
            this.nhom.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // dataGrid2
            // 
            this.dataGrid2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGrid2.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dataGrid2.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dataGrid2.CaptionBackColor = System.Drawing.SystemColors.Control;
            this.dataGrid2.CaptionForeColor = System.Drawing.Color.Red;
            this.dataGrid2.CaptionText = "xxx";
            this.dataGrid2.DataMember = "";
            this.dataGrid2.FlatMode = true;
            this.dataGrid2.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGrid2.Location = new System.Drawing.Point(8, 296);
            this.dataGrid2.Name = "dataGrid2";
            this.dataGrid2.RowHeaderWidth = 5;
            this.dataGrid2.Size = new System.Drawing.Size(392, 208);
            this.dataGrid2.TabIndex = 127;
            // 
            // dataGrid3
            // 
            this.dataGrid3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGrid3.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dataGrid3.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dataGrid3.CaptionBackColor = System.Drawing.SystemColors.Control;
            this.dataGrid3.CaptionForeColor = System.Drawing.Color.Red;
            this.dataGrid3.CaptionText = "xxx";
            this.dataGrid3.DataMember = "";
            this.dataGrid3.FlatMode = true;
            this.dataGrid3.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid3.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGrid3.Location = new System.Drawing.Point(408, 296);
            this.dataGrid3.Name = "dataGrid3";
            this.dataGrid3.RowHeaderWidth = 5;
            this.dataGrid3.Size = new System.Drawing.Size(376, 208);
            this.dataGrid3.TabIndex = 128;
            // 
            // chkAll
            // 
            this.chkAll.Appearance = System.Windows.Forms.Appearance.Button;
            this.chkAll.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.chkAll.Location = new System.Drawing.Point(25, 99);
            this.chkAll.Name = "chkAll";
            this.chkAll.Size = new System.Drawing.Size(23, 21);
            this.chkAll.TabIndex = 129;
            this.chkAll.Text = "...";
            this.chkAll.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.toolTip1.SetToolTip(this.chkAll, "Chọn cả");
            this.chkAll.CheckedChanged += new System.EventHandler(this.chkAll_CheckedChanged);
            // 
            // chkChitiet
            // 
            this.chkChitiet.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.chkChitiet.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.chkChitiet.Location = new System.Drawing.Point(7, 506);
            this.chkChitiet.Name = "chkChitiet";
            this.chkChitiet.Size = new System.Drawing.Size(88, 21);
            this.chkChitiet.TabIndex = 30;
            this.chkChitiet.Text = "Hiện chi tiết";
            this.chkChitiet.CheckedChanged += new System.EventHandler(this.chkChitiet_CheckedChanged);
            // 
            // c710
            // 
            this.c710.BackColor = System.Drawing.SystemColors.HighlightText;
            this.c710.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.c710.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.c710.Location = new System.Drawing.Point(48, 5);
            this.c710.Name = "c710";
            this.c710.Size = new System.Drawing.Size(168, 21);
            this.c710.TabIndex = 0;
            this.c710.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // label42
            // 
            this.label42.Location = new System.Drawing.Point(-16, 3);
            this.label42.Name = "label42";
            this.label42.Size = new System.Drawing.Size(64, 23);
            this.label42.TabIndex = 0;
            this.label42.Text = "Nhóm :";
            this.label42.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // c707
            // 
            this.c707.BackColor = System.Drawing.SystemColors.HighlightText;
            this.c707.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.c707.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.c707.Location = new System.Drawing.Point(48, 49);
            this.c707.Name = "c707";
            this.c707.Size = new System.Drawing.Size(168, 21);
            this.c707.TabIndex = 10;
            this.c707.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // label39
            // 
            this.label39.Location = new System.Drawing.Point(-13, 48);
            this.label39.Name = "label39";
            this.label39.Size = new System.Drawing.Size(61, 23);
            this.label39.TabIndex = 20;
            this.label39.Text = "Cấp phát :";
            this.label39.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // c706
            // 
            this.c706.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.c706.BackColor = System.Drawing.SystemColors.HighlightText;
            this.c706.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.c706.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.c706.Location = new System.Drawing.Point(280, 28);
            this.c706.Name = "c706";
            this.c706.Size = new System.Drawing.Size(317, 21);
            this.c706.TabIndex = 8;
            this.c706.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // label50
            // 
            this.label50.Location = new System.Drawing.Point(208, 23);
            this.label50.Name = "label50";
            this.label50.Size = new System.Drawing.Size(72, 23);
            this.label50.TabIndex = 16;
            this.label50.Text = "Loại khoản :";
            this.label50.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // c705
            // 
            this.c705.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.c705.BackColor = System.Drawing.SystemColors.HighlightText;
            this.c705.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.c705.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.c705.Location = new System.Drawing.Point(680, 28);
            this.c705.Name = "c705";
            this.c705.Size = new System.Drawing.Size(104, 21);
            this.c705.TabIndex = 9;
            this.c705.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // label38
            // 
            this.label38.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.label38.Location = new System.Drawing.Point(597, 24);
            this.label38.Name = "label38";
            this.label38.Size = new System.Drawing.Size(77, 23);
            this.label38.TabIndex = 18;
            this.label38.Text = "Loại b.kê :";
            this.label38.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // c703
            // 
            this.c703.BackColor = System.Drawing.SystemColors.HighlightText;
            this.c703.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.c703.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.c703.Location = new System.Drawing.Point(48, 28);
            this.c703.Name = "c703";
            this.c703.Size = new System.Drawing.Size(168, 21);
            this.c703.TabIndex = 7;
            this.c703.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // c702
            // 
            this.c702.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.c702.BackColor = System.Drawing.SystemColors.HighlightText;
            this.c702.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.c702.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.c702.Location = new System.Drawing.Point(680, 5);
            this.c702.Name = "c702";
            this.c702.Size = new System.Drawing.Size(104, 21);
            this.c702.TabIndex = 2;
            this.c702.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // label36
            // 
            this.label36.Location = new System.Drawing.Point(-16, 23);
            this.label36.Name = "label36";
            this.label36.Size = new System.Drawing.Size(64, 23);
            this.label36.TabIndex = 14;
            this.label36.Text = "N. vụ :";
            this.label36.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label37
            // 
            this.label37.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.label37.Location = new System.Drawing.Point(624, 3);
            this.label37.Name = "label37";
            this.label37.Size = new System.Drawing.Size(56, 23);
            this.label37.TabIndex = 4;
            this.label37.Text = "Chương :";
            this.label37.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // c700
            // 
            this.c700.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.c700.BackColor = System.Drawing.SystemColors.HighlightText;
            this.c700.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.c700.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.c700.Location = new System.Drawing.Point(280, 5);
            this.c700.Name = "c700";
            this.c700.Size = new System.Drawing.Size(317, 21);
            this.c700.TabIndex = 1;
            this.c700.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // label35
            // 
            this.label35.Location = new System.Drawing.Point(216, 3);
            this.label35.Name = "label35";
            this.label35.Size = new System.Drawing.Size(64, 23);
            this.label35.TabIndex = 2;
            this.label35.Text = "Hoạt động :";
            this.label35.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label7
            // 
            this.label7.Enabled = false;
            this.label7.Location = new System.Drawing.Point(768, 551);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(10, 23);
            this.label7.TabIndex = 6;
            this.label7.Text = "K. mục 1 :";
            this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.label7.Visible = false;
            // 
            // khoanmuc
            // 
            this.khoanmuc.BackColor = System.Drawing.SystemColors.HighlightText;
            this.khoanmuc.Enabled = false;
            this.khoanmuc.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.khoanmuc.Location = new System.Drawing.Point(784, 553);
            this.khoanmuc.Name = "khoanmuc";
            this.khoanmuc.Size = new System.Drawing.Size(10, 21);
            this.khoanmuc.TabIndex = 3;
            this.khoanmuc.Visible = false;
            this.khoanmuc.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // khoanmuc2
            // 
            this.khoanmuc2.BackColor = System.Drawing.SystemColors.HighlightText;
            this.khoanmuc2.Enabled = false;
            this.khoanmuc2.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.khoanmuc2.Location = new System.Drawing.Point(784, 555);
            this.khoanmuc2.Name = "khoanmuc2";
            this.khoanmuc2.Size = new System.Drawing.Size(10, 21);
            this.khoanmuc2.TabIndex = 4;
            this.khoanmuc2.Visible = false;
            this.khoanmuc2.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // label8
            // 
            this.label8.Enabled = false;
            this.label8.Location = new System.Drawing.Point(768, 553);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(10, 23);
            this.label8.TabIndex = 8;
            this.label8.Text = "K. mục 2 :";
            this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.label8.Visible = false;
            // 
            // khoanmuc4
            // 
            this.khoanmuc4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.khoanmuc4.BackColor = System.Drawing.SystemColors.HighlightText;
            this.khoanmuc4.Enabled = false;
            this.khoanmuc4.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.khoanmuc4.Location = new System.Drawing.Point(784, 555);
            this.khoanmuc4.Name = "khoanmuc4";
            this.khoanmuc4.Size = new System.Drawing.Size(10, 21);
            this.khoanmuc4.TabIndex = 6;
            this.khoanmuc4.Visible = false;
            this.khoanmuc4.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // label9
            // 
            this.label9.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.label9.Enabled = false;
            this.label9.Location = new System.Drawing.Point(768, 553);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(10, 23);
            this.label9.TabIndex = 12;
            this.label9.Text = "K. mục 4 :";
            this.label9.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.label9.Visible = false;
            // 
            // khoanmuc3
            // 
            this.khoanmuc3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.khoanmuc3.BackColor = System.Drawing.SystemColors.HighlightText;
            this.khoanmuc3.Enabled = false;
            this.khoanmuc3.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.khoanmuc3.Location = new System.Drawing.Point(784, 555);
            this.khoanmuc3.Name = "khoanmuc3";
            this.khoanmuc3.Size = new System.Drawing.Size(10, 21);
            this.khoanmuc3.TabIndex = 5;
            this.khoanmuc3.Visible = false;
            this.khoanmuc3.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // label10
            // 
            this.label10.Enabled = false;
            this.label10.Location = new System.Drawing.Point(770, 555);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(10, 23);
            this.label10.TabIndex = 10;
            this.label10.Text = "K. mục 3 :";
            this.label10.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.label10.Visible = false;
            // 
            // butIn
            // 
            this.butIn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butIn.Image = ((System.Drawing.Image)(resources.GetObject("butIn.Image")));
            this.butIn.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butIn.Location = new System.Drawing.Point(394, 512);
            this.butIn.Name = "butIn";
            this.butIn.Size = new System.Drawing.Size(84, 25);
            this.butIn.TabIndex = 232;
            this.butIn.Text = "&Phiếu nhập";
            this.butIn.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butIn.Click += new System.EventHandler(this.butIn_Click);
            // 
            // frmImpnhap
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(792, 573);
            this.Controls.Add(this.butIn);
            this.Controls.Add(this.c700);
            this.Controls.Add(this.khoanmuc2);
            this.Controls.Add(this.khoanmuc4);
            this.Controls.Add(this.label9);
            this.Controls.Add(this.khoanmuc3);
            this.Controls.Add(this.label10);
            this.Controls.Add(this.label8);
            this.Controls.Add(this.khoanmuc);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.c703);
            this.Controls.Add(this.c705);
            this.Controls.Add(this.c710);
            this.Controls.Add(this.label42);
            this.Controls.Add(this.c707);
            this.Controls.Add(this.label39);
            this.Controls.Add(this.c706);
            this.Controls.Add(this.label50);
            this.Controls.Add(this.label38);
            this.Controls.Add(this.c702);
            this.Controls.Add(this.label36);
            this.Controls.Add(this.label37);
            this.Controls.Add(this.label35);
            this.Controls.Add(this.chkChitiet);
            this.Controls.Add(this.chkAll);
            this.Controls.Add(this.nhom);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.butTonghop);
            this.Controls.Add(this.butKetthuc);
            this.Controls.Add(this.butLuu);
            this.Controls.Add(this.madv);
            this.Controls.Add(this.sohd);
            this.Controls.Add(this.sophieu);
            this.Controls.Add(this.den);
            this.Controls.Add(this.tu);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.dataGrid1);
            this.Controls.Add(this.dataGrid2);
            this.Controls.Add(this.dataGrid3);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.KeyPreview = true;
            this.Name = "frmImpnhap";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Thu nhập chứng từ phiếu nhập kho";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Load += new System.EventHandler(this.frmImpnhap_Load);
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.frmImpnhap_MouseMove);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid3)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void frmImpnhap_Load(object sender, System.EventArgs e)
		{
			try
			{
				user=a.user;bStkhangnam=a.bStkhangnam();
				iHoatdong=a.iHoatdong();iLctug=a.iLctug();iChuong=a.iChuong();iNghiepvu=a.iNghiepvu();iLoaibk=a.iLoaibk();
                iLk = a.iLk(); iCapphat = a.iCapphat(); sNoidung = a.sNoidung(); iUserid = a.iUserid(); iNhom = a.iNhom(); iTaikhoanchiuthue = a.iTaikhoanchiuthue();

				dataGrid2.CaptionText=dataGrid3.CaptionText="";
				c700.DisplayMember="TEN";
				c700.ValueMember="ID";
				c700.DataSource=a.get_data("select * from a_hoatdong where sudung=1 order by stt").Tables[0];
				c700.SelectedValue=iHoatdong;

				c702.DisplayMember="TEN";
				c702.ValueMember="ID";
				c702.DataSource=a.get_data("select * from a_dmchuong where sudung=1 order by stt").Tables[0];
				c702.SelectedValue=iChuong;

				c703.DisplayMember="TEN";
				c703.ValueMember="ID";
				c703.DataSource=a.get_data("select * from a_nghiepvu where sudung=1 order by stt").Tables[0];
				c703.SelectedValue=iNghiepvu;	
	
				c705.DisplayMember="TEN";
				c705.ValueMember="ID";
				c705.DataSource=a.get_data("select * from a_dmloaibk where sudung=1 order by stt").Tables[0];
				c705.SelectedValue=iLoaibk;
            
				c706.DisplayMember="LK";
				c706.ValueMember="ID";
				c706.DataSource=a.get_data("select id,lk||' '||tlk as lk,tlk from a_dmlk order by stt").Tables[0];
				c706.SelectedValue=iLk;

				c707.DisplayMember="TEN";
				c707.ValueMember="ID";
				c707.DataSource=a.get_data("select * from a_dmcapphat where sudung=1 order by stt").Tables[0];
				c707.SelectedValue=iCapphat;

				c710.DisplayMember="TEN";
				c710.ValueMember="ID";
				c710.DataSource=a.get_data("select * from a_dmnhombc order by stt").Tables[0];
				c710.SelectedValue=iNhom;

				nhom.DisplayMember="TEN";
				nhom.ValueMember="ID";
				nhom.DataSource=a.get_data("select * from "+d.user+".d_dmnhomkho order by stt").Tables[0];

                khoanmuc.DisplayMember = "TEN";
                khoanmuc.ValueMember = "ID";
                khoanmuc.DataSource = a.get_data("select * from a_dmkhoanmuc where sudung=1 order by loaikm,stt").Tables[0];
                

				khoanmuc2.DisplayMember="TEN";
				khoanmuc2.ValueMember="ID";
				khoanmuc2.DataSource=a.get_data("select * from a_dmkhoanmuc where sudung=1 order by loaikm,stt").Tables[0];

				khoanmuc3.DisplayMember="TEN";
				khoanmuc3.ValueMember="ID";
				khoanmuc3.DataSource=a.get_data("select * from a_dmkhoanmuc where sudung=1 order by loaikm,stt").Tables[0];

				khoanmuc4.DisplayMember="TEN";
				khoanmuc4.ValueMember="ID";
				khoanmuc4.DataSource=a.get_data("select * from a_dmkhoanmuc where sudung=1 order by loaikm,stt").Tables[0];

				tu.Value=new DateTime(2000+int.Parse(s_mmyy.Substring(2,2)),int.Parse(s_mmyy.Substring(0,2)),1,0,0,0);
				den.Value=new DateTime(int.Parse(s_ngay.Substring(6,4)),int.Parse(s_ngay.Substring(3,2)),int.Parse(s_ngay.Substring(0,2)),0,0,0);
				ds.ReadXml("..\\..\\..\\xml\\d_thanhtoan.xml");
				ds.Tables[0].Columns.Add("nguon");
				ds.Tables[0].Columns.Add("kho");
				ds.Tables[0].Columns.Add("Chon",typeof(bool));
				ds.Tables[0].Columns.Add("macongno",typeof(decimal));
				ds.Tables[0].Columns.Add("stkkho",typeof(decimal));
				ds.Tables[0].Columns.Add("tcnkp",typeof(decimal));
				ds.Tables[0].Columns.Add("userid",typeof(decimal));
				load_grid();
				AddGridTableStyle();
			
				this.disabledBackBrush = new SolidBrush(Color.FromArgb(255,255,192));
				this.disabledTextBrush = new SolidBrush(Color.FromArgb(255,0,0));

				this.alertBackBrush = new SolidBrush(SystemColors.HotTrack);
				this.alertFont = new Font(this.dataGrid1.Font.Name, this.dataGrid1.Font.Size, FontStyle.Bold);
				this.alertTextBrush = new SolidBrush(Color.White);
			
				this.currentRowFont = new Font(this.dataGrid1.Font.Name, this.dataGrid1.Font.Size, FontStyle.Regular);
				this.currentRowBackBrush = new SolidBrush(Color.FromArgb(0,255, 255));		

				dttk=a.get_data("select * from a_dmtk order by stk").Tables[0];

				string _mmyy=s_mmyy;
				//if (!a.bdMmyy(_mmyy))
                if (!d.bMmyy(_mmyy))
				{
					foreach(DataRow r in a.get_data("select * from "+d.user+".d_table order by substr(mmyy,3,2) desc,substr(mmyy,1,2) desc").Tables[0].Rows)
					{
						_mmyy=r["mmyy"].ToString();break;
					}
				}
				load_ct(_mmyy,-1);
				AddGridTableStyle1();
				AddGridTableStyle2();

				chkChitiet.Checked=a.Thongso("impct")=="1";
			}
			catch
			{
				MessageBox.Show("không kết nối được với kho",a.Msg);
			}
		}

		private void load_ct(string mmyy,long id)
		{
			if (id==-1 || chkChitiet.Checked)
			{
				sql = "select a.stt,a.mabd,b.ma,trim(b.ten)||' '||b.hamluong ten,nvl(b.tenhc,' ') tenhc,b.donvi,b.dang,a.handung,a.losx,a.soluong,a.dongia,a.vat,a.sotien,a.sotien+a.sotien*a.vat/100 as sotienvat,a.giaban,a.giamua,c.ten tenhang,d.ten tennuoc,a.sl1,a.sl2,a.tyle,a.cuocvc,a.chaythu,a.namsx,a.tailieu,a.baohanh,a.nguongoc,a.tinhtrang,a.sothe,a.giabancu,a.giamuacu,0 as tylemua ";
				sql+=" from "+d.user+mmyy+".d_nhapct a,"+d.user+".d_dmbd b,"+d.user+".d_dmhang c,"+d.user+".d_dmnuoc d ";				
                sql += " where a.mabd=b.id and b.mahang=c.id and b.manuoc=d.id and a.id=" + id + " order by a.stt";
				dsct=a.get_data(sql);
				dataGrid2.DataSource=dsct.Tables[0];
				CurrencyManager cm = (CurrencyManager)BindingContext[dataGrid2.DataSource,dataGrid2.DataMember]; 
				DataView dv = (DataView) cm.List; 
				dv.AllowNew = false; 
				load_nhom(mmyy,id);
			}
			try
			{
				string s="Số phiếu : "+dataGrid1[dataGrid1.CurrentCell.RowNumber,4].ToString()+" ngày "+dataGrid1[dataGrid1.CurrentCell.RowNumber,5].ToString();
				dataGrid2.CaptionText=dataGrid3.CaptionText=s;
			}
			catch{}
		}

		private void load_nhom(string mmyy,long id)
		{
            //sql="select e.ma,e.ten,sum(a.sotien+a.sotien*a.vat/100+a.cuocvc+a.chaythu) as sotien ";
            //sql+=" from "+user+"d"+mmyy+".d_nhapct a,"+user+".d_dmbd b,"+user+".d_dmhang c,"+user+".d_dmnuoc d,"+user+".d_dmnhomkt e ";
            //sql+=" where a.mabd=b.id and b.mahang=c.id and b.manuoc=d.id and b.sotk=e.id(+) and a.id="+id+" group by e.ma,e.ten order by e.ma,e.ten";
            //dsnhom=a.get_data(sql);

            sql = "select e.ma,e.ten,sum(a.sotien+a.sotien*a.vat/100+a.cuocvc+a.chaythu) as sotien ";
            sql += " from " + d.user + mmyy + ".d_nhapct a," + d.user + ".d_dmbd b," + d.user + ".d_dmhang c," + d.user + ".d_dmnuoc d," + d.user + ".d_dmnhomkt e ";
            sql += " where a.mabd=b.id and b.mahang=c.id and b.manuoc=d.id and b.sotk=e.id(+) and a.id=" + id + " group by e.ma,e.ten order by e.ma,e.ten";
            dsnhom = a.get_data(sql);

			dataGrid3.DataSource=dsnhom.Tables[0];
			CurrencyManager cm = (CurrencyManager)BindingContext[dataGrid3.DataSource,dataGrid3.DataMember]; 
			DataView dv = (DataView) cm.List; 
			dv.AllowNew = false; 
		}

		private void AddGridTableStyle1()
		{
			DataGridTableStyle ts =new DataGridTableStyle();
			ts.MappingName = dsct.Tables[0].TableName;
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
			ts.ReadOnly=false;
			ts.RowHeaderWidth=10;
						
			DataGridTextBoxColumn TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "tt";
			TextCol.HeaderText = "STT";
			TextCol.Width = 30;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ma";
			TextCol.HeaderText = "Mã số";
			TextCol.Width = 50;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ten";
			TextCol.HeaderText = "Tên";
			TextCol.Width = 200;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "dang";
			TextCol.HeaderText = "ĐVT";
			TextCol.Width = 50;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "soluong";
			TextCol.HeaderText = "Số lượng";
			TextCol.Width = 60;
			TextCol.Format="### ### ##0.00";
			TextCol.Alignment=HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "dongia";
			TextCol.HeaderText = "Đơn giá";
			TextCol.Width = 90;
			TextCol.Format="### ### ### ##0.00";
			TextCol.Alignment=HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "sotien";
			TextCol.HeaderText = "Số tiền";
			TextCol.Width = 100;
			TextCol.Format="### ### ### ### ##0.00";
			TextCol.Alignment=HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "vat";
			TextCol.HeaderText = "Thuế";
			TextCol.Width = 30;
			TextCol.Format="##0";
			TextCol.Alignment=HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "sotienvat";
			TextCol.HeaderText = "Số tiền+Thuế";
			TextCol.Width = 100;
			TextCol.Format="### ### ### ### ##0.00";
			TextCol.Alignment=HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "handung";
			TextCol.HeaderText = "Date";
			TextCol.Width = 30;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "losx";
			TextCol.HeaderText = "Lô SX";
			TextCol.Width = 100;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "tenhang";
			TextCol.HeaderText = "Hãng";
			TextCol.Width = 120;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "tennuoc";
			TextCol.HeaderText = "Nước";
			TextCol.Width = 120;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);
		}

		private void AddGridTableStyle2()
		{
			DataGridTableStyle ts =new DataGridTableStyle();
			ts.MappingName = dsnhom.Tables[0].TableName;
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
			ts.ReadOnly=false;
			ts.RowHeaderWidth=10;
						
			DataGridTextBoxColumn TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ma";
			TextCol.HeaderText = "Số tài khoản";
			TextCol.Width = 80;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid3.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ten";
			TextCol.HeaderText = "Tên tài khoản";
			TextCol.Width = 175;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid3.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "sotien";
			TextCol.HeaderText = "Số tiền";
			TextCol.Width = 100;
			TextCol.Format="### ### ### ### ##0.00";
			TextCol.Alignment=HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid3.TableStyles.Add(ts);
		}

		private void load_grid()
		{
			ds.Clear();dsct.Clear();
			DateTime dt1=a.StringToDate(tu.Text).AddDays(-a.iNgaykiemke);
			DateTime dt2=a.StringToDate(den.Text).AddDays(a.iNgaykiemke);
			int y1=dt1.Year,m1=dt1.Month;
			int y2=dt2.Year,m2=dt2.Month;
			int itu,iden;
			string mmyy="";
			for(int i=y1;i<=y2;i++)
			{
				itu=(i==y1)?m1:1;
				iden=(i==y2)?m2:12;
				for(int j=itu;j<=iden;j++)
				{
					mmyy=j.ToString().PadLeft(2,'0')+i.ToString().Substring(2,2);
					//if (a.bdMmyy(mmyy))
                    if (d.bMmyy(mmyy))
					{
                    //    Nếu bệnh viện đang theo dõi có User "D" thì chạy câu sql này
                    //    sql="select a.id,e.ten as nguon,a.sophieu,to_char(a.ngaysp,'dd/mm/yyyy') ngaysp,a.sohd,to_char(a.ngayhd,'dd/mm/yyyy') ngayhd,";
                    //    sql+="c.ten madv,nvl(b.no,a.no) no,nvl(b.co,a.co) co,b.sotien,a.ketoan,d.ten as tenkho,nvl(c.macongno,0) as macongno,nvl(f.id,0) as tkco,nvl(e.ketoan,1) as tcnkp,d.useridkt";
                    //    sql+=" from "+user+"d"+mmyy+".d_nhapll a,"+user+"d"+mmyy+".d_thanhtoan b,"+user+".d_dmnx c,"+user+".d_dmkho d,"+user+".d_dmnguon e,"+user+".a_dmtk f";
                    //    sql+=" where a.id=b.id and a.madv=c.id and a.makho=d.id and a.loai='M' and b.sotien<>0 and a.nhom="+int.Parse(nhom.SelectedValue.ToString())+" and a.manguon=e.id(+) and d.tkco=f.stk(+)";
                    //    sql+=" and to_date(a.ngaysp,'dd/mm/yy') between to_date('"+tu.Text+"','dd/mm/yy') and to_date('"+den.Text+"','dd/mm/yy')";
                    //    sql+=" order by a.sophieu,a.ngaysp";
                    //    foreach(DataRow r in a.get_data(sql).Tables[0].Rows) a.updrec_congno(ds.Tables[0],mmyy,long.Parse(r["id"].ToString()),r["sophieu"].ToString(),r["ngaysp"].ToString(),r["sohd"].ToString(),r["ngayhd"].ToString(),r["madv"].ToString(),r["no"].ToString(),r["co"].ToString(),decimal.Parse(r["sotien"].ToString()),decimal.Parse(r["ketoan"].ToString()),r["tenkho"].ToString(),long.Parse(r["macongno"].ToString()),long.Parse(r["tkco"].ToString()),int.Parse(r["tcnkp"].ToString()),(int.Parse(r["useridkt"].ToString())==0)?iUserid:int.Parse(r["useridkt"].ToString()),r["nguon"].ToString());
                    //

                    // Đặt trưng của bệnh viện 115 là không sử dụng User "D" tháng năm nên sử dụng câu query này
                        sql = "select a.id,e.ten as nguon,a.sophieu,to_char(a.ngaysp,'dd/mm/yyyy') ngaysp,a.sohd,to_char(a.ngayhd,'dd/mm/yyyy') ngayhd,";
                        sql += "c.ten madv,nvl(b.no,a.no) no,nvl(b.co,a.co) co,b.sotien,a.ketoan,d.ten as tenkho,nvl(c.macongno,0) as macongno,nvl(f.id,0) as tkco,nvl(e.ketoan,1) as tcnkp,d.useridkt";
                        sql += " from " + d.user + mmyy + ".d_nhapll a," + d.user + mmyy + ".d_thanhtoan b," + d.user + ".d_dmnx c," + d.user + ".d_dmkho d," + d.user + ".d_dmnguon e," + user + ".a_dmtk f";
                        sql += " where a.id=b.id and a.madv=c.id and a.makho=d.id and a.loai='M' and b.sotien<>0 and a.nhom=" + int.Parse(nhom.SelectedValue.ToString()) + " and a.manguon=e.id(+) and d.tkco=f.stk(+)";
                        sql += " and to_date(a.ngaysp,'dd/mm/yy') between to_date('" + tu.Text + "','dd/mm/yy') and to_date('" + den.Text + "','dd/mm/yy')";
                        sql += " order by a.sophieu,a.ngaysp";
                        foreach (DataRow r in a.get_data(sql).Tables[0].Rows) a.updrec_congno(ds.Tables[0], mmyy, long.Parse(r["id"].ToString()), r["sophieu"].ToString(), r["ngaysp"].ToString(), r["sohd"].ToString(), r["ngayhd"].ToString(), r["madv"].ToString(), r["no"].ToString(), r["co"].ToString(), decimal.Parse(r["sotien"].ToString()), decimal.Parse(r["ketoan"].ToString()), r["tenkho"].ToString(), long.Parse(r["macongno"].ToString()), long.Parse(r["tkco"].ToString()), int.Parse(r["tcnkp"].ToString()), (int.Parse(r["useridkt"].ToString()) == 0) ? iUserid : int.Parse(r["useridkt"].ToString()), r["nguon"].ToString());
                        
                    }
				}
			}
			dataGrid1.DataSource=ds.Tables[0];
			CurrencyManager cm = (CurrencyManager)BindingContext[dataGrid1.DataSource,dataGrid1.DataMember]; 
			DataView dv = (DataView) cm.List; 
			dv.AllowNew = false; 
			try
			{				
				load_ct(dataGrid1[dataGrid1.CurrentCell.RowNumber,1].ToString(),long.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber,2].ToString()));
			}
			catch{}
		}

        //private void AddGridTableStyle()
        //{
        //    DataGridTableStyle ts =new DataGridTableStyle();
        //    ts.MappingName = ds.Tables[0].TableName;
        //    ts.AlternatingBackColor = Color.Beige;
        //    ts.BackColor = Color.GhostWhite;
        //    ts.ForeColor = Color.MidnightBlue;
        //    ts.GridLineColor = Color.RoyalBlue;
        //    ts.HeaderBackColor = Color.MidnightBlue;
        //    ts.HeaderForeColor = Color.Lavender;
        //    ts.SelectionBackColor = Color.FromArgb(0,255,255);
        //    ts.SelectionForeColor = Color.PaleGreen;
        //    ts.RowHeaderWidth=5;
					
        //    FormattableBooleanColumn discontinuedCol = new FormattableBooleanColumn();
        //    discontinuedCol.MappingName = "chon";
        //    discontinuedCol.HeaderText = "Chọn";
        //    discontinuedCol.Width = 30;
        //    discontinuedCol.AllowNull = false;

        //    discontinuedCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
        //    discontinuedCol.BoolValueChanged += new BoolValueChangedEventHandler(BoolValueChanged);
        //    ts.GridColumnStyles.Add(discontinuedCol);
        //    dataGrid1.TableStyles.Add(ts);

        //    FormattableTextBoxColumn TextCol = new FormattableTextBoxColumn();
        //    TextCol.MappingName = "mmyy";
        //    TextCol.HeaderText = "";
        //    TextCol.Width =0;
        //    ts.GridColumnStyles.Add(TextCol);
        //    dataGrid1.TableStyles.Add(ts);

        //    TextCol = new FormattableTextBoxColumn();
        //    TextCol.MappingName = "id";
        //    TextCol.HeaderText = "";
        //    TextCol.Width =0;
        //    ts.GridColumnStyles.Add(TextCol);
        //    dataGrid1.TableStyles.Add(ts);

        //    TextCol = new FormattableTextBoxColumn();
        //    TextCol.MappingName = "nguon";
        //    TextCol.HeaderText = "Nguồn";
        //    TextCol.Width =100;
        //    TextCol.ReadOnly=true;
        //    TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
        //    ts.GridColumnStyles.Add(TextCol);
        //    dataGrid1.TableStyles.Add(ts);

        //    TextCol = new FormattableTextBoxColumn();
        //    TextCol.MappingName = "kho";
        //    TextCol.HeaderText = "Kho";
        //    TextCol.Width =100;
        //    TextCol.ReadOnly=true;
        //    TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
        //    ts.GridColumnStyles.Add(TextCol);
        //    dataGrid1.TableStyles.Add(ts);

        //    TextCol = new FormattableTextBoxColumn();
        //    TextCol.MappingName = "sophieu";
        //    TextCol.HeaderText = "Số phiếu";
        //    TextCol.Width =60;
        //    TextCol.ReadOnly=true;
        //    TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
        //    ts.GridColumnStyles.Add(TextCol);
        //    dataGrid1.TableStyles.Add(ts);

        //    TextCol=new FormattableTextBoxColumn();
        //    TextCol.MappingName = "ngaysp";
        //    TextCol.HeaderText = "Ngày";
        //    TextCol.Width = 65;
        //    TextCol.ReadOnly=true;
        //    TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
        //    ts.GridColumnStyles.Add(TextCol);
        //    dataGrid1.TableStyles.Add(ts);

        //    TextCol=new FormattableTextBoxColumn();
        //    TextCol.MappingName = "sohd";
        //    TextCol.HeaderText = "Số hóa đơn";
        //    TextCol.Width = 100;
        //    TextCol.ReadOnly=true;
        //    TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
        //    ts.GridColumnStyles.Add(TextCol);
        //    dataGrid1.TableStyles.Add(ts);

        //    TextCol=new FormattableTextBoxColumn();
        //    TextCol.MappingName = "ngayhd";
        //    TextCol.HeaderText = "Ngày HĐ";
        //    TextCol.Width = 65;			
        //    TextCol.ReadOnly=true;
        //    TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
        //    ts.GridColumnStyles.Add(TextCol);
        //    dataGrid1.TableStyles.Add(ts);

        //    TextCol=new FormattableTextBoxColumn();
        //    TextCol.MappingName = "madv";
        //    TextCol.HeaderText = "Nhà cung cấp";
        //    //TextCol.Width =dataGrid1.Width-(15+20+30+100+60+65+100+65+100+50+100+100);
        //    TextCol.Width = 150;
        //    TextCol.ReadOnly=true;
        //    TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
        //    ts.GridColumnStyles.Add(TextCol);
        //    dataGrid1.TableStyles.Add(ts);

        //    TextCol=new FormattableTextBoxColumn();
        //    TextCol.MappingName = "no";
        //    TextCol.HeaderText = "Nợ";
        //    TextCol.Width =50;
        //    TextCol.ReadOnly=false;
        //    TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
        //    ts.GridColumnStyles.Add(TextCol);
        //    dataGrid1.TableStyles.Add(ts);

        //    TextCol=new FormattableTextBoxColumn();
        //    TextCol.MappingName = "co";
        //    TextCol.HeaderText = "Có";
        //    TextCol.Width = 50;
        //    TextCol.ReadOnly=false;
        //    TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
        //    ts.GridColumnStyles.Add(TextCol);
        //    dataGrid1.TableStyles.Add(ts);

        //    TextCol=new FormattableTextBoxColumn();
        //    TextCol.MappingName = "sotien";
        //    TextCol.HeaderText = "Số tiền";
        //    TextCol.Format="### ### ### ##0.00";
        //    TextCol.Width =100;
        //    TextCol.ReadOnly=true;
        //    TextCol.Alignment=HorizontalAlignment.Right;
        //    //TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
        //    ts.GridColumnStyles.Add(TextCol);
        //    dataGrid1.TableStyles.Add(ts);

        //    TextCol=new FormattableTextBoxColumn();
        //    TextCol.MappingName = "datra";
        //    TextCol.HeaderText = "";
        //    TextCol.Width =0;
        //    ts.GridColumnStyles.Add(TextCol);
        //    dataGrid1.TableStyles.Add(ts);
        //}

        private void AddGridTableStyle()
        {
            DataGridTableStyle ts = new DataGridTableStyle();
            ts.MappingName = ds.Tables[0].TableName;
            ts.AlternatingBackColor = Color.Beige;
            ts.BackColor = Color.GhostWhite;
            ts.ForeColor = Color.MidnightBlue;
            ts.GridLineColor = Color.RoyalBlue;
            ts.HeaderBackColor = Color.MidnightBlue;
            ts.HeaderForeColor = Color.Lavender;
            ts.SelectionBackColor = Color.FromArgb(0, 255, 255);
            ts.SelectionForeColor = Color.PaleGreen;
            ts.RowHeaderWidth = 5;

            FormattableBooleanColumn discontinuedCol = new FormattableBooleanColumn();
            discontinuedCol.MappingName = "chon";
            discontinuedCol.HeaderText = "Chọn";
            discontinuedCol.Width = 30;
            discontinuedCol.AllowNull = false;

            discontinuedCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
            discontinuedCol.BoolValueChanged += new BoolValueChangedEventHandler(BoolValueChanged);
            ts.GridColumnStyles.Add(discontinuedCol);
            dataGrid1.TableStyles.Add(ts);

            FormattableTextBoxColumn TextCol = new FormattableTextBoxColumn();
            TextCol.MappingName = "mmyy";
            TextCol.HeaderText = "";
            TextCol.Width = 0;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new FormattableTextBoxColumn();
            TextCol.MappingName = "id";
            TextCol.HeaderText = "";
            TextCol.Width = 0;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new FormattableTextBoxColumn();
            TextCol.MappingName = "kho";
            TextCol.HeaderText = "Kho";
            TextCol.Width = 100;
            TextCol.ReadOnly = true;
            TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new FormattableTextBoxColumn();
            TextCol.MappingName = "sophieu";
            TextCol.HeaderText = "Số phiếu";
            TextCol.Width = 60;
            TextCol.ReadOnly = true;
            TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new FormattableTextBoxColumn();
            TextCol.MappingName = "ngaysp";
            TextCol.HeaderText = "Ngày";
            TextCol.Width = 65;
            TextCol.ReadOnly = true;
            TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new FormattableTextBoxColumn();
            TextCol.MappingName = "sohd";
            TextCol.HeaderText = "Số hóa đơn";
            TextCol.Width = 100;
            TextCol.ReadOnly = true;
            TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new FormattableTextBoxColumn();
            TextCol.MappingName = "ngayhd";
            TextCol.HeaderText = "Ngày";
            TextCol.Width = 65;
            TextCol.ReadOnly = true;
            TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new FormattableTextBoxColumn();
            TextCol.MappingName = "madv";
            TextCol.HeaderText = "Nhà cung cấp";
            TextCol.Width = dataGrid1.Width - (20 + 30 + 100 + 60 + 65 + 100 + 65 + 100 + 50 + 100 + 100);
            TextCol.ReadOnly = true;
            TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new FormattableTextBoxColumn();
            TextCol.MappingName = "no";
            TextCol.HeaderText = "Nợ";
            TextCol.Width = 100;
            TextCol.ReadOnly = false;
            TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new FormattableTextBoxColumn();
            TextCol.MappingName = "co";
            TextCol.HeaderText = "Có";
            TextCol.Width = 50;
            TextCol.ReadOnly = false;
            TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new FormattableTextBoxColumn();
            TextCol.MappingName = "sotien";
            TextCol.HeaderText = "Số tiền";
            TextCol.Format = "### ### ### ##0.00";
            TextCol.Width = 100;
            TextCol.ReadOnly = true;
            TextCol.Alignment = HorizontalAlignment.Right;
            //TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);
        }
	
	
		private void SetCellFormat(object sender, DataGridFormatCellEventArgs e)
		{
			try
			{
				bool discontinued = (bool) ((e.Column != 0) ? this.dataGrid1[e.Row, 0] : e.CurrentCellValue);
				if(e.Column > 0 && (bool)(this.dataGrid1[e.Row, 0]))//discontinued)
				{
					e.ForeBrush = this.disabledTextBrush;
				}
				else if(e.Column > 0 && e.Row == this.dataGrid1.CurrentRowIndex)
				{
					e.BackBrush = this.currentRowBackBrush;
					e.TextFont = this.currentRowFont;
				}
				if (decimal.Parse(dataGrid1[e.Row,13].ToString())!=0) e.ForeBrush=new SolidBrush(Color.Orange);
			}
			catch{}
		}

		private void BoolValueChanged(object sender, BoolValueChangedEventArgs e)
		{
			this.dataGrid1.EndEdit(this.dataGrid1.TableStyles[0].GridColumnStyles["Discontinued"],e.Row, false);
			RefreshRow(e.Row);
			this.dataGrid1.BeginEdit(this.dataGrid1.TableStyles[0].GridColumnStyles["Discontinued"],e.Row);
		}
		private void RefreshRow(int row)
		{
			Rectangle rect = this.dataGrid1.GetCellBounds(row, 0);
			rect = new Rectangle(rect.Right, rect.Top, this.dataGrid1.Width, rect.Height);
			this.dataGrid1.Invalidate(rect);
		}

		private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
		{
			if((bool)this.dataGrid1[this.dataGrid1.CurrentRowIndex, checkCol])
				this.dataGrid1.CurrentCell = new DataGridCell(this.dataGrid1.CurrentRowIndex, checkCol);
			afterCurrentCellChanged = true;
			try
			{
				load_ct(dataGrid1[dataGrid1.CurrentCell.RowNumber,1].ToString(),long.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber,2].ToString()));
			}
			catch{}
		}

		private void dataGrid1_Click(object sender, System.EventArgs e)
		{
			Point pt = this.dataGrid1.PointToClient(Control.MousePosition);
			DataGrid.HitTestInfo hti = this.dataGrid1.HitTest(pt);
			BindingManagerBase bmb = this.BindingContext[this.dataGrid1.DataSource, this.dataGrid1.DataMember];
			if(afterCurrentCellChanged && hti.Row < bmb.Count 
				&& hti.Type == DataGrid.HitTestType.Cell 
				&&  hti.Column == checkCol )
			{	
				this.dataGrid1[hti.Row, checkCol] = !(bool)this.dataGrid1[hti.Row, checkCol];
				RefreshRow(hti.Row);
			}
			afterCurrentCellChanged = false;
		}

		private void tu_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");
		}

		private void sophieu_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==sophieu) ref_grid();
		}

		private void sohd_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==sohd) ref_grid();
		}

		private void madv_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==madv) ref_grid();
		}

		private void ref_grid()
		{
			CurrencyManager cm= (CurrencyManager)BindingContext[dataGrid1.DataSource];
			DataView dv=(DataView)cm.List;
			sql="";
			if (sophieu.Text!="")
			{
				if (sql!="") sql+=" and ";
				sql+=" sophieu like '%"+sophieu.Text+"%'";
			}
			if (sohd.Text!="")
			{
				if (sql!="") sql+=" and ";
				sql+=" sohd like '%"+sohd.Text+"%'";
			}
			if (madv.Text!="")
			{
				if (sql!="") sql+=" and ";
				sql+=" madv like '%"+madv.Text+"%'";
			}
			dv.RowFilter=sql;
		}

		private void butTonghop_Click(object sender, System.EventArgs e)
		{
			format_sotien=a.format_sotien(int.Parse(nhom.SelectedValue.ToString()));
			butKiemtra_Click(sender,e);
			load_grid();
		}

		private bool kiemtra()
		{
			if (a.bKhoaso(s_mmyy))
			{
				MessageBox.Show("Số liệu tháng "+s_mmyy.Substring(0,2)+" năm 20"+s_mmyy.Substring(2)+" đã khóa sổ !",a.Msg);
				tu.Focus();
				return false;
			}
			string s="";
            foreach (DataRow r in ds.Tables[0].Select("chon=true and macongno = 0 and datra=0")) //and macongno = 0
			{
				if (s.IndexOf(r["madv"].ToString().Trim())==-1) s+=r["madv"].ToString().Trim()+"\n";
			}
			if (s =="")
			{
				MessageBox.Show("Những nhà cung cấp sau chưa khai báo tương đương với khách hàng của kế toán :\n"+s,a.Msg);
				tu.Focus();
				return false;
			}			
			foreach(DataRow r in ds.Tables[0].Select("chon=true and stkkho <> 0 and datra=0"))
				if (s.IndexOf(r["kho"].ToString().Trim())==-1) s+=r["kho"].ToString().Trim()+"\n";
			if (s =="")
			{
				MessageBox.Show("Số tài khoản có các kho sau không hợp lệ :\n"+s,a.Msg);
				tu.Focus();
				return false;
			}
			foreach(DataRow r in ds.Tables[0].Select("chon=true and datra=0"))
			{
				sql="select f.id,b.ma,trim(b.ten)||' '||trim(b.hamluong) as ten,b.dang ";
				sql+=" from "+d.user+r["mmyy"].ToString()+".d_nhapct a,"+d.user+".d_dmbd b,"+d.user+".d_dmhang c,"+d.user+".d_dmnuoc d,"+d.user+".d_dmnhomkt e,"+user+".a_dmtk f";
				sql+=" where a.mabd=b.id and b.mahang=c.id and b.manuoc=d.id and b.sotk=e.id(+) and e.ma=f.stk(+) and a.id="+long.Parse(r["id"].ToString());
				sql+=" and (f.id is null or b.sotk is null)";
				foreach(DataRow r2 in a.get_data(sql).Tables[0].Rows)
					if (s.IndexOf(r2["ten"].ToString().Trim()+" "+r2["dang"].ToString().Trim())==-1) s+=r2["ten"].ToString().Trim()+" "+r2["dang"].ToString().Trim()+"\n";
			}
			if (s =="")
			{
				MessageBox.Show("Những mặt hàng sau khai báo tài khoản không hợp lệ :\n"+s,a.Msg);
				tu.Focus();
				return false;
			}
			if (bStkhangnam)
			{
				DataRow r1;string xxx="",stk;
				foreach(DataRow r in ds.Tables[0].Rows)
				{
					xxx=d.user+r["mmyy"].ToString();
					if (r["chon"].ToString().Trim().ToLower()=="true" && decimal.Parse(r["datra"].ToString())==0)
					{
						sql="select f.id,h.stk,e.ma,e.ten,sum(a.sotien+a.sotien*a.vat/100+a.cuocvc+a.chaythu) as sotien ";
						sql+=" from "+xxx+".d_nhapct a,"+d.user+".d_dmbd b,"+d.user+".d_dmhang c,"+d.user+".d_dmnuoc d,";
						sql+=d.user+".d_dmnhomkt e,"+user+".a_dmtk f,"+xxx+".d_nhapll g,"+d.user+".d_dmnguon h";
						sql+=" where g.id=a.id and g.manguon=h.id and a.mabd=b.id and b.mahang=c.id and b.manuoc=d.id ";
						sql+=" and b.sotk=e.id and e.ma=f.stk and a.id="+long.Parse(r["id"].ToString());
						sql+=" group by f.id,h.stk,e.ma,e.ten order by f.id,h.stk,e.ma,e.ten";
						foreach(DataRow r2 in a.get_data(sql).Tables[0].Rows)
						{
							stk=r2["ma"].ToString();
							if (r2["stk"].ToString()=="1") stk=stk.Substring(0,stk.Length-1)+"2";
							r1=a.getrowbyid(dttk,"stk='"+stk+"'");
							if (r1==null) s+=stk+"\n";
						}
					}
				}
				if (s =="")
				{
					MessageBox.Show("Số tài khoản nợ sau không hợp lệ :\n"+s,a.Msg);
					tu.Focus();
					return false;
				}
			}
			iHoatdong=int.Parse(c700.SelectedValue.ToString());
			iChuong=int.Parse(c702.SelectedValue.ToString());
			iNghiepvu=int.Parse(c703.SelectedValue.ToString());
			iLoaibk=int.Parse(c705.SelectedValue.ToString());
			iLk=int.Parse(c706.SelectedValue.ToString());
			iCapphat=int.Parse(c707.SelectedValue.ToString());
			iNhom=int.Parse(c710.SelectedValue.ToString());
			return true;
		}

		private void butLuu_Click(object sender, System.EventArgs e)
		{
			CurrencyManager cm= (CurrencyManager)BindingContext[dataGrid1.DataSource];
			DataView dv=(DataView)cm.List;
			dv.RowFilter="";
			if (!kiemtra()) return;
			long idct=0,idctct=0,tknoa;
			int stt=1;
			string xxx,stk,ttk;
			bool bUpdate=false;
            decimal stvat=0;
			DataRow r1;
			foreach(DataRow r in ds.Tables[0].Rows)
			{
				if (r["chon"].ToString().Trim().ToLower()=="true" && decimal.Parse(r["datra"].ToString())==0)
				{
					idct=a.getIdct();
                    xxx=d.user+r["mmyy"].ToString();
					a.updCt(s_mmyy,idct,iNhom,"",iChuong,iChucnang,iHoatdong,iLctug,r["sohd"].ToString(),r["ngayhd"].ToString(),s_ngay,sNoidung,0,long.Parse(r["stkkho"].ToString()),1,long.Parse(r["macongno"].ToString()),Math.Round(decimal.Parse(r["sotien"].ToString()),0),0,s_mmyy.Substring(0,2),iNghiepvu,iLoaibk,int.Parse(r["tcnkp"].ToString()),0,"",0,int.Parse(r["userid"].ToString()));

                    //sql = "select f.id,h.stk,e.ma,e.ten,sum(a.sotien+a.sotien*a.vat/100+a.cuocvc+a.chaythu) as sotien ";
                    //sql += " from " + xxx + ".d_nhapct a," + d.user + ".d_dmbd b," + d.user + ".d_dmhang c," + d.user + ".d_dmnuoc d,";
                    //sql += d.user + ".d_dmnhomkt e," + user + ".a_dmtk f," + xxx + ".d_nhapll g," + d.user + ".d_dmnguon h";
                    //sql += " where g.id=a.id and g.manguon=h.id and a.mabd=b.id and b.mahang=c.id and b.manuoc=d.id ";
                    //sql += " and b.sotk=e.id and e.ma=f.stk and a.id=" + long.Parse(r["id"].ToString());
                    //sql += " group by f.id,h.stk,e.ma,e.ten order by f.id,h.stk,e.ma,e.ten";
                    //stt = 1;
                    sql = "select f.id,e.ma,";
                    if (chkChitiet.Checked) sql += "trim(b.ten)||' '||trim(b.hamluong)||' '||b.dang as ten,a.sotien*a.vat/100 as sotienvat,a.sotien+a.cuocvc+a.chaythu as sotien ";
                    else sql += "e.ten,sum(a.sotien*a.vat/100) as sotienvat,sum(a.sotien+a.cuocvc+a.chaythu) as sotien ";
                    sql += " from " + xxx + ".d_nhapct a," + d.user + ".d_dmbd b," + d.user + ".d_dmhang c," + d.user + ".d_dmnuoc d," + d.user + ".d_dmnhomkt e," + user + ".a_dmtk f";
                    sql += " where a.mabd=b.id and b.mahang=c.id and b.manuoc=d.id and b.sotk=e.id and e.ma=f.stk ";
                    sql += " and a.id=" + long.Parse(r["id"].ToString());
                    if (!chkChitiet.Checked) sql += " group by f.id,e.ten order by f.id,e.ten";
                    stt = 1;
					foreach(DataRow r2 in a.get_data(sql).Tables[0].Rows)
					{
						ttk=r2["ten"].ToString();
						idctct=a.getIdctct();
                        tknoa=long.Parse(r2["id"].ToString());
                        stvat = decimal.Parse(r2["sotienvat"].ToString());
                        sTaikhoanchiuthue = a.sTaikhoanchiuthue(iTaikhoanchiuthue);

                        //if (bStkhangnam) 
                        //{
                        //    stk=r2["ma"].ToString();
                        //    if (r2["stk"].ToString()=="0")	stk=stk.Substring(0,stk.Length-1)+"2";//"1"
                        //    r1=a.getrowbyid(dttk,"stk='"+stk+"'");
                        //    if (r1!=null)
                        //    {
                        //        tknoa=long.Parse(r1["id"].ToString());
                        //        ttk=r1["ttk"].ToString();
                        //    }
                        //}
                        // Kiểm tra xem có thuế gtgt hay không để định khoản khấu trừ luôn
                        if (stvat != 0 && iTaikhoanchiuthue != 0)
                        {
                            a.updCtct(s_mmyy, idctct, idct, stt++, iLk, ttk, tknoa, long.Parse(r["stkkho"].ToString()), (khoanmuc.SelectedIndex == -1) ? 0 : long.Parse(khoanmuc.SelectedValue.ToString()), 0, (khoanmuc2.SelectedIndex == -1) ? 0 : long.Parse(khoanmuc2.SelectedValue.ToString()), 0, (khoanmuc3.SelectedIndex == -1) ? 0 : long.Parse(khoanmuc3.SelectedValue.ToString()), 0, (khoanmuc4.SelectedIndex == -1) ? 0 : long.Parse(khoanmuc4.SelectedValue.ToString()), 0, 0, 0, Math.Round(decimal.Parse(r2["sotien"].ToString()), 0), 0, 0, 1, iTonghop, 0, iCapphat, iLctug, "", r["ngaysp"].ToString(), iChuong, 0, iNghiepvu, long.Parse(r["macongno"].ToString()), iHoatdong, int.Parse(r["userid"].ToString()));
                            a.updCtct(s_mmyy, a.getIdctct(), idct, stt++, iLk, sTaikhoanchiuthue, iTaikhoanchiuthue, long.Parse(r["stkkho"].ToString()), (khoanmuc.SelectedIndex == -1) ? 0 : long.Parse(khoanmuc.SelectedValue.ToString()), 0, (khoanmuc2.SelectedIndex == -1) ? 0 : long.Parse(khoanmuc2.SelectedValue.ToString()), 0, (khoanmuc3.SelectedIndex == -1) ? 0 : long.Parse(khoanmuc3.SelectedValue.ToString()), 0, (khoanmuc4.SelectedIndex == -1) ? 0 : long.Parse(khoanmuc4.SelectedValue.ToString()), 0, 0, 0,stvat, 0, 0, 1, iTonghop, 0, iCapphat, iLctug, "", r["ngaysp"].ToString(), iChuong, 0, iNghiepvu, long.Parse(r["macongno"].ToString()), iHoatdong, int.Parse(r["userid"].ToString()));
                        }
                        else
                        {
                            a.updCtct(s_mmyy, idctct, idct, stt++, iLk, ttk, tknoa, long.Parse(r["stkkho"].ToString()), (khoanmuc.SelectedIndex == -1) ? 0 : long.Parse(khoanmuc.SelectedValue.ToString()), 0, (khoanmuc2.SelectedIndex == -1) ? 0 : long.Parse(khoanmuc2.SelectedValue.ToString()), 0, (khoanmuc3.SelectedIndex == -1) ? 0 : long.Parse(khoanmuc3.SelectedValue.ToString()), 0, (khoanmuc4.SelectedIndex == -1) ? 0 : long.Parse(khoanmuc4.SelectedValue.ToString()), 0, 0, 0, Math.Round(decimal.Parse(r2["sotien"].ToString()), 0), 0, 0, 1, iTonghop, 0, iCapphat, iLctug, "", r["ngaysp"].ToString(), iChuong, 0, iNghiepvu, long.Parse(r["macongno"].ToString()), iHoatdong, int.Parse(r["userid"].ToString()));
                        }
                        // Kiểm tra có tổng hợp để lên báo cáo hay không
                        //if (iTonghop==1)
                        //{
                        //    a.updSodu("a_sd",a.insert,s_mmyy,iNhom,iChuong,0,iLk,iNghiepvu,long.Parse(r["macongno"].ToString()),
                        //        tknoa,long.Parse(r["stkkho"].ToString()),(khoanmuc.SelectedIndex==-1)?0:long.Parse(khoanmuc.SelectedValue.ToString()),0,(khoanmuc2.SelectedIndex==-1)?0:long.Parse(khoanmuc2.SelectedValue.ToString()),0,(khoanmuc3.SelectedIndex==-1)?0:long.Parse(khoanmuc3.SelectedValue.ToString()),0,(khoanmuc4.SelectedIndex==-1)?0:long.Parse(khoanmuc4.SelectedValue.ToString()),0,0,0,iHoatdong,Math.Round(decimal.Parse(r2["sotien"].ToString()),0));
                        //    a.updSodu("a_sdc",a.insert,s_mmyy,iNhom,iChuong,0,iLk,iNghiepvu,long.Parse(r["macongno"].ToString()),
                        //        tknoa,long.Parse(r["stkkho"].ToString()),(khoanmuc.SelectedIndex==-1)?0:long.Parse(khoanmuc.SelectedValue.ToString()),0,(khoanmuc2.SelectedIndex==-1)?0:long.Parse(khoanmuc2.SelectedValue.ToString()),0,(khoanmuc3.SelectedIndex==-1)?0:long.Parse(khoanmuc3.SelectedValue.ToString()),0,(khoanmuc4.SelectedIndex==-1)?0:long.Parse(khoanmuc4.SelectedValue.ToString()),0,0,0,iHoatdong,Math.Round(decimal.Parse(r2["sotien"].ToString()),0));
                        //    if (a.bChophanbo(tknoa,long.Parse(r["stkkho"].ToString())))
                        //        a.updPhanbo("a_phanbochua",a.insert,idct,idctct,iLctug,r["sohd"].ToString(),r["ngayhd"].ToString(),
                        //            tknoa,long.Parse(r["stkkho"].ToString()),"",r2["ten"].ToString(),1,Math.Round(decimal.Parse(r2["sotien"].ToString()),0),
                        //            iNghiepvu,s_mmyy,int.Parse(r["userid"].ToString()));
                        //}
					}
					a.execute_data("update "+user+r["mmyy"].ToString()+".d_nhapll set ketoan="+idct+" where id="+long.Parse(r["id"].ToString()));
					a.upd_theodoihd(long.Parse(r["id"].ToString()),s_ngay,r["sophieu"].ToString(),r["ngaysp"].ToString(),r["sohd"].ToString(),r["ngayhd"].ToString(),sNoidung,long.Parse(r["macongno"].ToString()),Math.Round(decimal.Parse(r["sotien"].ToString()),0),long.Parse(r["id"].ToString()),0,0,r["mmyy"].ToString(),int.Parse(r["userid"].ToString()));
				}
				else if (r["chon"].ToString().Trim().ToLower()=="false" && decimal.Parse(r["datra"].ToString())!=0)
				{
					a.execute_data("delete from "+user+".a_ctct"+s_mmyy.Substring(2)+" where id="+long.Parse(r["datra"].ToString()));
					a.execute_data("delete from "+user+".a_ct"+s_mmyy.Substring(2)+" where id="+long.Parse(r["datra"].ToString()));
                    a.execute_data("update " + d.user + r["mmyy"].ToString() + ".d_nhapll set ketoan=0 where id=" + long.Parse(r["id"].ToString()));
					a.execute_data("delete from "+user+".a_theodoihdct where id="+long.Parse(r["id"].ToString()));
					a.execute_data("delete from "+user+".a_theodoihd where id="+long.Parse(r["id"].ToString()));
				}
				bUpdate=true;
			}
			if (bUpdate) a.updDtsd("",s_mmyy,iUserid);
			load_grid();		
			butTonghop.Focus();
		}

		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			a.close();System.GC.Collect();this.Close();
		}

		private void butKiemtra_Click(object sender, System.EventArgs e)
		{
			DateTime dt1=a.StringToDate(tu.Text).AddDays(-a.iNgaykiemke);
			DateTime dt2=a.StringToDate(den.Text).AddDays(a.iNgaykiemke);
			int y1=dt1.Year,m1=dt1.Month;
			int y2=dt2.Year,m2=dt2.Month;
			int itu,iden;
			string mmyy="";
			for(int i=y1;i<=y2;i++)
			{
				itu=(i==y1)?m1:1;
				iden=(i==y2)?m2:12;
				for(int j=itu;j<=iden;j++)
				{
					mmyy=j.ToString().PadLeft(2,'0')+i.ToString().Substring(2,2);
					if (d.bMmyy(mmyy))
					{
						sql="select a.id,sum(b.sotien+b.sotien*b.vat/100+b.cuocvc+b.chaythu) as sotien ";
						sql+=" from "+d.user+mmyy+".d_nhapll a,"+d.user+mmyy+".d_nhapct b where a.id=b.id and a.nhom="+int.Parse(nhom.SelectedValue.ToString())+" and a.loai='M'";
						sql+=" group by a.id";
						foreach(DataRow r in a.get_data(sql).Tables[0].Rows)
						{
                            //a.execute_data("update "+d.user+mmyy+".d_thanhtoan set sotien=0 where id="+long.Parse(r["id"].ToString()));
                            //a.upd_thanhtoan(mmyy,long.Parse(r["id"].ToString()),decimal.Parse(r["sotien"].ToString()));
						}
					}
				}
			}
		}

		private void chkChitiet_CheckedChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==chkChitiet) a.writeXml("a_thongso","impct",(chkChitiet.Checked)?"1":"0");
		}

		private void chkAll_CheckedChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==chkAll)
				foreach(DataRow r in ds.Tables[0].Select("datra=0")) r["chon"]=(chkAll.Checked)?true:false;
		}

		private void frmImpnhap_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (bClear)
			{
				khoanmuc2.SelectedIndex=khoanmuc3.SelectedIndex=khoanmuc4.SelectedIndex=-1;
				bClear=false;
			}
		}

		private void butIn_Click(object sender, System.EventArgs e)
		{
			try
			{
				string xxx=d.user+dataGrid1[dataGrid1.CurrentCell.RowNumber,1].ToString();
				long id=long.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber,2].ToString());
				sql="select a.stt,a.mabd,b.ma,trim(b.ten)||' '||b.hamluong ten,nvl(b.tenhc,' ') tenhc,b.donvi,b.dang,";
				sql+="a.handung,a.losx,a.soluong,a.dongia,a.vat,a.sotien,round(a.sotien+a.sotien*a.vat/100,0) sotienvat,";
				sql+="a.giaban,a.giamua,c.ten tenhang,d.ten tennuoc,a.sl1,a.sl2,a.tyle,a.cuocvc,a.chaythu,a.namsx,a.tailieu,a.baohanh,";
				sql+="a.nguongoc,a.tinhtrang,a.sothe,a.giabancu,a.giamuacu,round(decode(a.giamuacu,0,0,(a.giamua-a.giamuacu)/a.giamuacu*100),2) as tylemua ";
				sql+=" from "+xxx+".d_nhapct a,"+d.user+".d_dmbd b,"+d.user+".d_dmhang c,"+d.user+".d_dmnuoc d ";
				sql+=" where a.mabd=b.id and b.mahang=c.id and b.manuoc=d.id and a.id="+id+" order by a.stt";
				DataTable dttmp=a.get_data(sql).Tables[0];
				string c11,c12,_dc,_maso,sohd,nguoigiao,ngayhd,tendv,sophieu,ngaysp,makho;
				c11=c12=_dc=_maso=sohd=nguoigiao=ngayhd=tendv=sophieu=ngaysp=makho="";
				foreach(DataRow r in a.get_data("select a.nguoigiao,a.sophieu,to_char(a.ngaysp,'dd/mm/yyyy') as ngaysp,a.sohd,to_char(a.ngayhd,'dd/mm/yyyy') as ngayhd,a.no,a.co,c.ten as makho,b.* from "+xxx+".d_nhapll a,"+d.user+".d_dmnx b,"+d.user+".d_dmkho c where a.madv=b.id and a.makho=c.id and a.id="+id).Tables[0].Rows)
				{
					c11=r["no"].ToString();c12=r["co"].ToString();_dc=r["diachi"].ToString().Trim();_maso=r["masothue"].ToString().Trim();
					sohd=r["sohd"].ToString();ngayhd=r["ngayhd"].ToString();nguoigiao=r["nguoigiao"].ToString();tendv=r["ten"].ToString();
					sophieu=r["sophieu"].ToString();ngaysp=r["ngaysp"].ToString();makho=r["makho"].ToString();
				}
				string tenfile="d_phieunhap.rpt";
				if (c11.IndexOf(",")==-1) c11+=",";
				decimal st=0;
				string s12="NỢ :     ";
				sql="select c.ma,sum(a.sotien+a.sotien*a.vat/100) sotienvat from "+xxx+".d_nhapct a,";
				sql+=d.user+".d_dmbd b,"+d.user+".d_dmnhomkt c where a.mabd=b.id and b.sotk=c.id and a.id="+id+" group by c.ma";
				foreach(DataRow r1 in a.get_data(sql).Tables[0].Rows)
				{
					st=decimal.Parse(r1["sotienvat"].ToString());
					s12=s12+r1["ma"].ToString().Trim()+" :     "+st.ToString("###,###,###,###,####").Trim()+";      ";
				}
				c12=c12+","+s12;
				frmReport f=new frmReport(a,dttmp,tenfile,sophieu,ngaysp,c11,c12,nguoigiao,sohd,ngayhd,tendv,makho,doiso.Doiso_Unicode(Convert.ToInt64(st).ToString()),_dc,_maso);
				f.ShowDialog();
			}
			catch{}
		}		
	}
}		
